package com.hqd.ch03.v27.spel.express;

import com.hqd.ch03.v27.spel.ast.SpelNodeImpl;
import com.hqd.ch03.v27.spel.standard.ExpressionState;

/**
 * 属性、方法父节点，支持多级访问
 */
public class CompoundExpression extends SpelNodeImpl {
    public CompoundExpression(int startPos, int endPos, SpelNodeImpl... expressionComponents) {
        super(startPos, endPos, expressionComponents);
    }

    @Override
    public Object getValue(ExpressionState state) {
        if (children.length == 1) {
            return children[0].getValue(state);
        }
        int count = this.children.length - 1;
        SpelNodeImpl nextNode = this.children[0];
        Object result = nextNode.getValue(state);
        for (int i = 1; i < count; i++) {
            state.pushActiveContextObject(result);
            nextNode = this.children[i];
            result = nextNode.getValue(state);
            state.popActiveContextObject();

        }
        state.pushActiveContextObject(result);
        nextNode = this.children[count];
        result = nextNode.getValue(state);
        state.popActiveContextObject();
        return result;
    }
}
